package com.gzk.oeno.ihm.menus.help;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import com.gzk.oeno.ihm.MainOenoFrame;
import com.gzk.utils.CustomException;
import com.gzk.utils.appli.IApplication;
import com.gzk.utils.ihm.UIUtils;

public class VersionAction extends AbstractAction {

	private final IApplication application; 
	
	protected VersionAction(IApplication application) {
		super("Version", UIUtils.loadSilentIcon("icons/help_16.png"));
		this.application = application;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			Object content = buildReleaseDialog();
			String vesion = application.getName() + " " + application.getVersion();
			JOptionPane.showMessageDialog(MainOenoFrame.INSTANCE, content, vesion, JOptionPane.PLAIN_MESSAGE);
		} catch (CustomException e1) {
			e1.showException();
		}
	}
	
	
	protected JScrollPane buildReleaseDialog() throws CustomException{
		JEditorPane editorPane = new JEditorPane();
		editorPane.setEditable(false);
		URL helpURL = ClassLoader.getSystemResource("releases.html");
		if (helpURL != null) {
		    try {
		        editorPane.setPage(helpURL);
		    } catch (IOException ex) {
		    	throw new CustomException("Erreur interne [lecture du fichier impossible]", ex);
		    }
		} else {
			throw new CustomException("Erreur interne [fichier manquant]");
		}

		JScrollPane releaseDialog = new JScrollPane(editorPane);
		releaseDialog.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		releaseDialog.setPreferredSize(new Dimension(400, 350));
		
		return releaseDialog;
	}
	
}
